#!/usr/bin/perl
# Count number of WWW visitors per day
#
# A visitor is a distinct IP number.
# A visitor who stays over the dateline gets both days credited.
#
use LogCount;

$linecount = 5000;
$usage = "Usage: $0 [-lines count]\n";
if ($ENV{COUNTER_HOST}) {
    $counterserver = $ENV{COUNTER_HOST};
} else {
    $counterserver = "counter.li.org";
}

@fields = qw(total visitors posters divers);

while ($ARGV[0] =~ /^-./) {
    $opt = shift;
    if ($opt eq "-days") {
	$daycount = shift;
    } elsif ($opt eq "-byhour") {
	$byhour = 1;
    } elsif ($opt eq "-to") {
	$todate = shift;
    } else {
	die $usage;
    }
}
if ($byhour && $daycount) {
    $daycount *= 24; # 1 day = 24 hours; external beauty, not internal...
}
while (<>) {
    chop;
    if (/^(\S+).* \[([^\]]+)\] (\S+)? ?"/) { #"
        $ip = $1;
        $server = $3;
        $command = $';
        if (!$server) {
           ++ $oldformatlines;
           next;
        } elsif ($server ne $counterserver) {
           ++ $notcounterlines;
           next;
        }
        $date = LogCount::apachetosimple($2, $byhour);
	if ($date !~ /^\d\d\d\d-/) { # must have 4-digit years
	    warn "Silly date:$date\n";
	    next;
	}
	++ $count{$date}{total};
        if (!$seen{$date}{$ip}) {
            ++ $count{$date}{visitors};
        } elsif ($seen{$date}{$ip} == 1) { # looking at > a single page
            ++ $count{$date}{divers};
        }
        ++ $seen{$date}{$ip};
	if ($count{$date}{total} == 1) {
	    $seqno{$date} = $seqno;
            ++ $seqno;
        }
        ($cmd, $file) = split(" ", $command);
        if ($cmd eq "POST") {
            if (!$posted{$date}{$ip}) {
               ++ $count{$date}{posters};
               ++ $posted{$date}{$ip};
            }
        }
        ++ $lines;
    } else {
	print STDERR "Unparsed: $_\n";
    }
}
warn "$lines lines counted\n";
print STDERR "$oldformatlines old format lines (no server)\n"
    if $oldformatlines;
print STDERR "$notcounterlines lines not marked with counter.li.org\n"
    if $notcounterlines;

LogCount::zerofilltimes(\%count, $byhour);

printf "#%-13s", "Date";
for $field (@fields) {
    printf " %5.5s", $field;
}
print "\n";
@keys = sort(keys(%count));
$nkeys = $#keys;
if ($daycount) {
    $skip = $nkeys - $daycount;
    print STDERR "Skipping $skip of $nkeys output lines\n";
}

my $linesprinted = 0;
for $date (sort keys(%count)) {
    next if ($skip-- > 0);
#    next if $date lt "1990"; # Delete some rubbish dates
    last if $todate && $date ge $todate;
    printf "%10s ", $date;
    for $field (@fields) {
	printf " %5d", $count{$date}{$field};
    }
    ++ $linesprinted;
    print "\n";
}
warn "Date causing skip: $date ge $todate\n" if $date ge $todate;
warn "$linesprinted lines printed\n";
